package com.TinyBBS.model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.TinyBBS.bean.Game;
import com.TinyBBS.db.DBCRUD;
import com.TinyBBS.db.PageableResultSet;

public class GameModel {
	private DBCRUD crud = null;
	private ResultSet rs = null;
	private PageableResultSet prs = null;

	public GameModel() {
		crud = new DBCRUD();
		prs = new PageableResultSet();
	}

	public List<Game> getAllGame() {
		List<Game> gameList = new ArrayList<Game>();
		try {
			rs = crud.fetchAll("game");
			while (rs.next()) {
				Game game = new Game();
				game.setId(rs.getInt("id"));
				game.setCompany(rs.getString("company"));
				game.setName(rs.getString("name"));
				game.setSummary(rs.getString("summary"));
				game.setWebsite(rs.getString("website"));
				game.setImage(rs.getString("image"));
				game.setRating(rs.getInt("rating"));
				game.setReleaseDate(rs.getDate("releaseDate"));
				game.setUser_id(rs.getInt("user_id"));
				gameList.add(game);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return gameList;
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	public Game getGameById(String id) {
		Game game = new Game();
		rs = crud.fetchAll("game", "id = " + "'" + id + "'");
		return this.setGamebyResultSet(game, rs);
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	public int getRateById(String id) {
		int rating = -1;
		try {
			rs = crud.fetchAll("game", "id = " + "'" + id + "'");
			if (rs.next()) {
				rating = rs.getInt("rating");
				System.out.println(rating + "from getRateById");
			}else{
				return -1;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rating;
	}

	public boolean setRateById(String id, int newRate) {
		Map<String, Object> maplist = new HashMap<String, Object>();
		int oldRate = this.getRateById(id);
		int rating = (newRate + oldRate) / 2;
		System.out.println(newRate + "+" + oldRate + "=" + rating);
		maplist.put("rating", rating);
		String where = "id = '" + id + "'";
		if (crud.update("game", maplist, where)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 
	 * @param game
	 * @param rs
	 * @return
	 */
	private Game setGamebyResultSet(Game game, ResultSet rs) {
		try {
			if (rs.next()) {
				game.setId(rs.getInt("id"));
				game.setCompany(rs.getString("company"));
				game.setName(rs.getString("name"));
				game.setSummary(rs.getString("summary"));
				game.setReleaseDate(rs.getDate("releaseDate"));
				game.setWebsite(rs.getString("website"));
				game.setRating(rs.getInt("rating"));
				game.setUser_id(rs.getInt("user_id"));
				game.setImage(rs.getString("image"));
			} else {
				return null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		return game;
	}

	/**
	 * Return List<Game> according to page number and page size
	 * 
	 * @param pageNumber
	 *            cursor of data record
	 * @param pageSize
	 *            size of one page
	 * @return List<Game>
	 */
	public List<Game> getEntryList(int pageNumber, int pageSize) {
		List<Game> gameList = new ArrayList<Game>();
		try {
			rs = crud.fetchAll("game");
			if (rs == null) {
				return null;
			}
			if (pageNumber < 1) {
				pageNumber = 1;
			}
			if (pageNumber > 1) {
				rs.absolute(pageNumber);
			}
			for (int i = 0; i < pageSize; i++) {
				if (rs.next()) {
					Game game = new Game();
					game.setId(rs.getInt("id"));
					game.setCompany(rs.getString("company"));
					game.setName(rs.getString("name"));
					game.setSummary(rs.getString("summary"));
					game.setWebsite(rs.getString("website"));
					game.setImage(rs.getString("image"));
					game.setReleaseDate(rs.getDate("releaseDate"));
					game.setUser_id(rs.getInt("user_id"));
					gameList.add(game);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return gameList;

	}
	/**
	 * Remove game
	 * @param id
	 * @return boolean true if deleted successfully, else false
	 */
	public boolean removeGame(String id){
		return crud.delete("game", "id = " + "'" + id + "'");
	}
}
